package cn.starrysky108.simpleerp.core.config;

import cn.starrysky108.simpleerp.core.constant.Constants;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.TimeZone;

/**
 * @author Carl Lee
 */
@Configuration
public class JacksonConfig {
    /**
     * Jackson全局转化BigDecimal和Long类型为String，解决前端 精度问题
     * 设置时区
     *
     * @return Jackson2ObjectMapperBuilderCustomizer 注入的对象
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {

        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
                .serializerByType(BigDecimal.class, ToStringSerializer.instance)
                .timeZone(TimeZone.getTimeZone("GMT+8"))
                .simpleDateFormat(Constants.DEFAULT_DATE_TIME_FORMAT)
                .serializerByType(Long.class, ToStringSerializer.instance);
    }

}
